package com.scau.hwadee.examination.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.*;
import com.scau.hwadee.examination.mapper.*;
import com.scau.hwadee.examination.service.ProfessionalManagementService;
import com.scau.hwadee.examination.utils.UniqueIDGeneratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: YHX
 * @date: 2019/7/28 20:12
 * @description:
 */
@Service
public class ProfessionalManagementServiceImpl implements ProfessionalManagementService {
    @Autowired
    private MajorclassMapper majorclassMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private ExaminerMapper examinerMapper;

    @Autowired
    private NationwidemajorMapper nationwidemajorMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    private SimpleDateFormat string2Date = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public School getSubjectSchool(String schoolId) {
        return schoolMapper.selectByPrimaryKey(schoolId);
    }

    @Override
    public int insertNationalMajorFromExcel(List<Nationwidemajor> nationwidemajors) {
        try {
            return nationwidemajorMapper.insertList(nationwidemajors);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Examiner> getAllMajorSubjectSchools(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(examinerMapper.selectAllExaminers(pageNum, pageSize));
    }

    @Override
    public List<Nationwidemajor> getAllNationalMajor2ExportExcel() {
        return nationwidemajorMapper.selectAllNationwidemajor2ExportExcel();
    }

    @Override
    public int updateMajorMessage(Major major) {
        try {
            return majorMapper.updateByPrimaryKey(major);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int insertExaminerList(JSONArray params) {
        List<Examiner> examiners = new ArrayList<>();
        for (int i = 1; i < params.size(); i++) {
            JSONObject data = params.getJSONObject(i);
            Examiner examiner = new Examiner();
            examiner.setExaminerId(new UniqueIDGeneratorUtils.UUIDUniqueIDGeneratorUtils().generatorId());
            examiner.setMvId(data.get("majorPlanVersionId").toString());
            examiner.setMjId(data.get("majorId").toString());
            examiner.setScId(data.get("schoolId").toString());
            examiner.setEmNum(i + 1);
            examiners.add(examiner);
        }
        try {
            return examinerMapper.insertExaminerList(examiners);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Major> getAllMajor2ExportExcel() {
        return majorMapper.selectAllMajors2ExportExcel();
    }

    /**
     * wrh
     */
    @Override
    public Major getMajorByPrimaryKey(String mjId) {
        return majorMapper.selectByPrimaryKey(mjId);
    }

    @Override
    public int updateMajorEnableOrDisable(List<Major> majors) {
        try {
            return majorMapper.updateMajorDisableOrEnable(majors);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteNationalMajorList(JSONArray params) {
        List<Nationwidemajor> nationwidemajors = new ArrayList<>();
        for (int i = 0; i < params.size(); i++) {
            Nationwidemajor nationwidemajor = new Nationwidemajor();
            nationwidemajor.setNmId(params.getJSONObject(i).get("nationwidemajorId").toString());
            nationwidemajors.add(nationwidemajor);
        }
        try {
            return nationwidemajorMapper.deleteNationalMajorList(nationwidemajors);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateSubjectSchoolOfMajorByMajorId(String schoolName, String majorId) {
        try {
            return majorMapper.setSubjectSchoolOfMajorByMajorId(schoolName, majorId);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int createNewNationalMajor(Nationwidemajor nationwidemajor) {
        try {
            return nationwidemajorMapper.insert(nationwidemajor);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Nationwidemajor> getAllNationalMajor(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Nationwidemajor> nationwidemajorList = nationwidemajorMapper.selectAllNationwidemajor(pageNum, pageSize);
        return new PageInfo<>(nationwidemajorList);
    }

    @Override
    public PageInfo<Major> getAllMajor(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorMapper.selectAllMajor(pageNum, pageSize));
    }

    @Override
    public int insertMajorFromExcel(List<Major> majorList){
        try {
            return majorMapper.insertMajorList(majorList);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Majorclass> getAllMajorClass(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Majorclass> majorclassList = majorclassMapper.listAllMajorClass(pageNum, pageSize);
        return new PageInfo<>(majorclassList);
    }

    @Override
    public int createNewMajor(Major major) {
        try {
            return majorMapper.insertSelective(major);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateMajorClass(String beforeName, String afterId, String afterName) {
        try {
            return majorclassMapper.updateMajorClass(beforeName, afterId, afterName);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int createNewMajorClass(Majorclass majorclass) {
        try {
            return majorclassMapper.insert(majorclass);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
